<template>
  <div class='xtx-carousel' @mouseenter="stop" @mouseleave="start">
    <ul class="carousel-body">
      <li class="carousel-item" :class="{fade:currentIndex === index}" v-for="(item,index) in swiper" :key='item.id'>
        <RouterLink to="/">
          <img :src="item.imgUrl" alt="">
        </RouterLink>
      </li>
    </ul>
    <!-- 左右两侧的箭头 -->
    <a @click='toggle(true)' href="javascript:;" class="carousel-btn prev"><i class="iconfont icon-angle-left"></i></a>
    <a @click='toggle(false)' href="javascript:;" class="carousel-btn next"><i class="iconfont icon-angle-right"></i></a>
    <!-- 底部的小圆点 -->
    <div class="carousel-indicator">
      <span @click='currentIndex=i-1' :class='{active: currentIndex===i-1}' v-for="i in swiper.length" :key="i"></span>
    </div>
  </div>
</template>

<script>
import { onBeforeUnmount, ref, watch } from 'vue'
export default {
  name: 'XtxCarousel',
  props: {
    // 轮播时间间隔
    duration: {
      type: Number,
      default: 1
    },
    // 控制自动轮播
    autoPlay: {
      type: Boolean,
      default: true
    },
    swiper: {
      type: Array,
      default: () => []
    }
  },
  setup (props) {
    // 当前显示的图片的索引
    const currentIndex = ref(0)

    // 自动轮播
    let timer = null
    const autoPlay = () => {
      timer = setInterval(() => {
        currentIndex.value += 1
        if (currentIndex.value >= props.swiper.length) {
          currentIndex.value = 0
        }
      }, props.duration * 1000)
    }

    // 重启
    const start = () => {
      // 启动之前，先清除之前的任务
      // if(timer) clearInterval(timer)
      if (props.autoPlay && props.swiper.length > 1) {
        autoPlay()
      }
    }

    // 暂停
    const stop = () => {
      if (timer) clearInterval(timer)
    }

    // 监控轮播图数据，如果有数据更新就轮播
    watch(() => props.swiper, () => {
      currentIndex.value = 0
      start()
    })

    // 左右手动切换
    const toggle = (type) => {
      if (type) {
        // 左移
        if (currentIndex.value >= 1) {
          currentIndex.value -= 1
        } else {
          // 超出左边界，从右侧开始
          currentIndex.value = props.swiper.length - 1
        }
      } else {
        // 右移
        if (currentIndex.value < props.swiper.length - 1) {
          currentIndex.value += 1
        } else {
          // 超出右侧边界，从左侧开始
          currentIndex.value = 0
        }
      }
    }

    onBeforeUnmount(() => {
      // 组件销毁前清除定时任务
      if (timer) clearInterval(timer)
    })
    return { currentIndex, start, stop, toggle }
  }
}
</script>
<style scoped lang="less">
.xtx-carousel {
  width: 100%;
  height: 100%;
  min-width: 300px;
  min-height: 150px;
  position: relative;
  .carousel {
    &-body {
      width: 100%;
      height: 100%;
    }
    &-item {
      width: 100%;
      height: 100%;
      position: absolute;
      left: 0;
      top: 0;
      opacity: 0;
      transition: opacity 0.5s linear;
      &.fade {
        opacity: 1;
        z-index: 1;
      }
      img {
        width: 100%;
        height: 100%;
      }
    }
    &-indicator {
      position: absolute;
      left: 0;
      bottom: 20px;
      z-index: 2;
      width: 100%;
      text-align: center;
      span {
        display: inline-block;
        width: 12px;
        height: 12px;
        background: rgba(0, 0, 0, 0.2);
        border-radius: 50%;
        cursor: pointer;
        ~ span {
          margin-left: 12px;
        }
        &.active {
          background: #fff;
        }
      }
    }
    &-btn {
      width: 44px;
      height: 44px;
      background: rgba(0, 0, 0, 0.2);
      color: #fff;
      border-radius: 50%;
      position: absolute;
      top: 228px;
      z-index: 2;
      text-align: center;
      line-height: 44px;
      opacity: 0;
      transition: all 0.5s;
      &.prev {
        left: 20px;
      }
      &.next {
        right: 20px;
      }
    }
  }
  &:hover {
    .carousel-btn {
      opacity: 1;
    }
  }
}
</style>
